package com.onlinevideo.servlet;

import cn.hutool.json.JSONUtil;
import com.onlinevideo.data.MyConst;
import com.onlinevideo.data.ResultBean;
import com.onlinevideo.entity.EmailInfo;
import com.onlinevideo.util.MailInfoUtils;
import com.onlinevideo.util.VCodeUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

public class EmailServlet extends BaseServlet{

    private PrintWriter writer = null ;
    protected void  sendEmailCode(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        writer = resp.getWriter();
        String email = req.getParameter("email");
        String vCode = VCodeUtils.getVCode(6);
        HttpSession session = req.getSession();
        session.setAttribute("emailVcode",vCode);
        session.setMaxInactiveInterval(60*5);
        EmailInfo emailInfo = new EmailInfo(email,"验证码测试", MyConst.EMAIL_VERIFICATION_INFORMATION[0]+vCode+MyConst.EMAIL_VERIFICATION_INFORMATION[1],false);
        Boolean sendStatus = MailInfoUtils.sendMailUtils(emailInfo);
        if(sendStatus){
            writer.write(JSONUtil.toJsonStr(ResultBean.scOperationSuccess("验证码已经发送")));
            return;
        }
        writer.write(JSONUtil.toJsonStr(ResultBean.scOperationError("验证码发送失败")));
    }

}
